package com.lwf.fatapi.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.lwf.fatapi.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 调试控制器 - 用于测试 token 相关问题
 *
 * @author lwf
 */
@Tag(name = "调试接口", description = "用于调试 token 相关问题")
@RestController
@RequestMapping("/debug")
public class DebugController {

    @Operation(summary = "调试 Token 信息")
    @GetMapping("/token")
    public Result<Map<String, Object>> debugToken(
            HttpServletRequest request,
            @RequestHeader(value = "Authorization", required = false) String authorization) {
        
        Map<String, Object> debugInfo = new HashMap<>();
        
        // 获取请求头信息
        debugInfo.put("authorization_header", authorization);
        debugInfo.put("satoken_header", request.getHeader("satoken"));
        
        // SA-Token 相关信息
        debugInfo.put("is_login", StpUtil.isLogin());
        debugInfo.put("token_value", StpUtil.getTokenValue());
        debugInfo.put("token_name", StpUtil.getTokenName());
        
        try {
            debugInfo.put("login_id", StpUtil.getLoginId());
        } catch (Exception e) {
            debugInfo.put("login_id_error", e.getMessage());
        }
        
        // 所有请求头
        Map<String, String> headers = new HashMap<>();
        request.getHeaderNames().asIterator().forEachRemaining(headerName -> {
            headers.put(headerName, request.getHeader(headerName));
        });
        debugInfo.put("all_headers", headers);
        
        return Result.success(debugInfo);
    }
}
